package com.android.sgzcommon.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.widget.RemoteViews;

import com.android.sugz.R;

import androidx.annotation.DrawableRes;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;

/**
 * @author sgz
 * @date 2021-02-25
 */
public class NotificationUtils {


    /**
     * @param context
     * @param smallIcon
     * @param title
     * @param text
     */
    public static void notificate(Context context, @DrawableRes int smallIcon, CharSequence title, CharSequence text, boolean bigText) {
        String packageName = context.getPackageName();
        notificate(context, packageName, 0, packageName, "", smallIcon, title, text, bigText, null, false);
    }

    /**
     * 显示通知，点击通知可以跳转特殊的Activity，不带返回堆栈，返回直接退出。
     *
     * @param context
     * @param smallIcon
     * @param title
     * @param text
     * @param bigText
     * @param activity
     */
    public static void notificateActivity(Context context, @DrawableRes int smallIcon, CharSequence title, CharSequence text, boolean bigText, Class<?> activity) {
        String packageName = context.getPackageName();
        notificate(context, packageName, 1, packageName, "", smallIcon, title, text, bigText, activity, false);
    }

    /**
     * 显示通知，点击通知可以跳转带有返回堆栈的Activity。注意，清单文件中的Activity添加android:parentActivityName属性。
     *
     * @param context
     * @param smallIcon
     * @param title
     * @param text
     * @param bigText
     * @param activity
     */
    public static void notificateStackActivity(Context context, @DrawableRes int smallIcon, CharSequence title, CharSequence text, boolean bigText, Class<?> activity) {
        String packageName = context.getPackageName();
        notificate(context, packageName, 2, packageName, "", smallIcon, title, text, bigText, activity, true);
    }

    /**
     * @param context
     * @param channelId
     * @param notificationId
     * @param channelName
     * @param channelDes
     * @param smallIcon
     * @param title
     * @param text
     * @param bigText
     * @param activity       即将跳转的Activity
     * @param isStack        是否跳转包含返回堆栈的Activity，True时，注意清单文件中的Activity添加android:parentActivityName属性。
     */
    public static void notificate(Context context, String channelId, int notificationId, String channelName, String channelDes, @DrawableRes int smallIcon, CharSequence title, CharSequence text, boolean bigText, Class<?> activity, boolean isStack) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
        builder.setSmallIcon(smallIcon);
        builder.setContentTitle(title);
        builder.setContentText(text);
        if (bigText) {
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(text));
        }
        if (activity != null) {
            PendingIntent pendingIntent;
            if (isStack) {
                /**
                 *     <activity
                 *         android:name=".ResultActivity"
                 *         android:parentActivityName=".MainActivity" />
                 *     </activity>
                 */
                Intent resultIntent = new Intent(context, activity);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addNextIntentWithParentStack(resultIntent);
                pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            } else {
                /**
                 *     <activity
                 *         android:name=".ResultActivity"
                 *         android:launchMode="singleTask"
                 *         android:taskAffinity=""
                 *         android:excludeFromRecents="true">
                 *     </activity>
                 */
                Intent intent = new Intent(context, activity);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            }
            builder.setContentIntent(pendingIntent);
        }
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setAutoCancel(true);
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(channelDes);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            notificationManager.createNotificationChannel(channel);
        }
        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(notificationId, builder.build());
    }

    /**
     * 显示新版本下载进度的通知。
     * @param context
     * @param title 标题
     * @param progress 下载进度值
     * @param resId 通知左边显示的图标
     */
    public static void versionNotification(Context context, String title, int progress, int resId) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        try {
            String channelId = "versionUpdate";
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(channelId, "下载进度", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(notificationChannel);
            }
            RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.layout_sgz_download_notification);
            contentView.setProgressBar(R.id.pb_progress, 100, progress, false);
            contentView.setTextViewText(R.id.tv_version, title);
            contentView.setTextViewText(R.id.tv_progress, progress + "%");
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
            builder.setCustomContentView(contentView);
            builder.setWhen(System.currentTimeMillis());
            builder.setSmallIcon(R.drawable.ic_sgz_download);
            builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), resId));
            Notification notification = builder.build();
            notification.contentView.setImageViewResource(R.id.iv_icon, resId);
            if (100 == progress) {
                notificationManager.cancel(1);
            } else {
                notificationManager.notify(1, notification);
            }
        } catch (Exception e) {
            e.printStackTrace();
            notificationManager.cancel(1);
        }
    }
}
